import { Ref } from "vue";
import FiledProperty from "@/models/FiledProperty";
import Scope from "@/models/Scope";
import { ElMessage, ElMessageBox } from "element-plus/es";
import api from "@/utils";

export default function FormEvent() {
  /**
   * 递归生成初始化新增时的submitForm
   * @param filedPropertyList 表单字段的各种属性
   */
  const initAddFormEvent = (filedPropertyList: Array<FiledProperty>) => {
    let temp = {};
    filedPropertyList.forEach((filed) => {
      if (filed.children) {
        // 如果是子表的话，则将该属性初始化为一个数组
        temp = Object.assign(temp, { [filed.filedName]: [] });
      } else {
        // 如果存在json属性, 则进行递归, 反之则设为空值.
        let objValue = null;
        if (filed.jsonFiled) {
          objValue = initAddFormEvent(filed.jsonFiled);
        } else if (filed.formType === "el-form-input-select") {
          objValue = {};
        }
        temp = Object.assign(temp, { [filed.filedName]: objValue });
      }
    });
    return temp;
  };

  /**
   * 点击添加按钮时执行的事件.
   * @param submitForm 表单
   * @param submitFormTitle 表单标题
   * @param filedPropertyList 传入的用来初始化表单的属性.
   */
  const clickAddBtn = (
    submitForm: Ref<Record<string, unknown>>,
    submitFormTitle: Ref<string>,
    filedPropertyList: Array<FiledProperty>
  ) => {
    submitForm.value = initAddFormEvent(filedPropertyList);
    console.log(submitForm.value);
    submitFormTitle.value = "新增";
  };

  /**
   * 点击编辑按钮执行的事件.
   * @param scoped 被点击行的信息.
   * @param dataIndex 表单索引.
   * @param submitForm 表单.
   * @param submitFormTitle 表单的标题.
   */
  const clickEditBtn = (
    scoped: Scope,
    dataIndex: Ref<number>,
    submitForm: Ref<Record<string, unknown>>,
    submitFormTitle: Ref<string>
  ) => {
    submitForm.value = JSON.parse(JSON.stringify(scoped.row));
    dataIndex.value = scoped.$index;
    submitFormTitle.value = "编辑";
  };

  /**
   * 执行删除表单操作
   * @param currentSelects
   * @param deleteUrl
   */
  const sendDeleteRequest = async (
    currentSelects: Array<Record<string, string>>,
    deleteUrl: string
  ) => {
    const selectMultipleId = currentSelects.map(
      (item: Record<string, unknown>) => item.id
    );
    await api.http
      .postJson(deleteUrl, selectMultipleId)
      .then(() => {
        ElMessage.success("删除成功!");
      })
      .catch((err) => {
        console.log(err);
      });
  };
  /**
   * 删除表单前的确认工作
   * @param currentSelects 当前选中的需要被删除的表单数据
   */
  const clickDeleteBtn = async (
    currentSelects: Array<Record<string, string>>
  ) => {
    const selectMultipleId = currentSelects.map(
      (item: Record<string, unknown>) => item.id
    );
    await ElMessageBox.confirm(
      '确认删除 "选中的 ' + selectMultipleId.length + ' 条" 记录吗？',
      "Warning",
      {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }
    );
  };
  /**
   * 批量作废表单数据前的确认工作
   * @param currentSelects 当前选中的需要被作废的表单数据
   */
  const clickInvalidBtn = async (
    currentSelects: Array<Record<string, string>>
  ) => {
    const selectMultipleId = currentSelects.map(
      (item: Record<string, unknown>) => item.id
    );
    await ElMessageBox.confirm(
      '确认作废 "选中的 ' + selectMultipleId.length + ' 条" 记录吗？',
      "Warning",
      {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }
    );
  };

  /**
   * 执行表单数据作废操作
   * @param currentSelects
   * @param invalidUrl
   */
  const sendInvalidRequest = async (
    currentSelects: Array<Record<string, string>>,
    invalidUrl: string
  ) => {
    const selectMultipleId = currentSelects.map(
      (item: Record<string, unknown>) => item.id
    );
    await api.http
      .postJson(invalidUrl, selectMultipleId)
      .then(() => {
        ElMessage.success("作废成功!");
      })
      .catch((err) => {
        console.log(err);
      });
  };
  return {
    clickAddBtn,
    clickEditBtn,
    clickDeleteBtn,
    clickInvalidBtn,
    sendDeleteRequest,
    sendInvalidRequest,
  };
}
